const _wx = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initemdata:[],
    value:'',
    newdata:[],
    newdataid:[],
    checked:0,
    num:0
  },
  onChanges(event){  
    this.setData({
      checked: event.detail,
    });
  },
  onChange(e) {
    this.setData({
      value: e.detail,
    });
  },
  newid_(e){
    console.log(e);
    this.setData({
      newdataid: e.target.dataset.value,
      checked:0
    });
    this.warehouse()
  },
  onClick(){
    var that = this
    _wx.getData('/admin/warehouse/noneedauth/inLibGoods', 'GET', {
      name:that.data.value
    }, function (res) {
      if(res.code == 0){
        that.setData({
          newdata:res.data,
          newdataid:res.data[0]
        })
        that.warehouse()
      }
    });
  },
  warehouse(){
    var that = this
    _wx.getData('/admin/warehouse/noneedauth/inventory', 'GET', {
      lib_id:that.data.newdataid.value
    }, function (res) {
      if(res.code == 0){
        that.setData({
          warehousesdata:res.data.map((item) => {
            return {
              count: item.count,
              id: item.id,
              name: item.name,
              uname: that.data.newdataid.name,
              tf: false,
              val: that.data.newdataid.value,
            };
          }),
        })
      }
    });
  },
  onnum(event){
    this.setData({
      num: event.detail,
    });
  },
  warehouses(){
    var that=this
    if(that.data.num <= 0){
      wx.showToast({
        title: '请选择数量',
        icon: 'none',
        duration: 2000
      });
      return
    }
    var list = that.data.warehousesdata[that.data.checked]

    that.setData({
      initemdata:that.data.initemdata.concat({
        number: that.data.num,
        id: list.id,
        name: list.name,
        uname: list.uname,
        val: list.val,
      }),
      value: "",
      warehousesdata: [],
      newdata:[],
      num: 0,
    })
    console.log(that.data.initemdata);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(JSON.parse(options.fieldValue).length > 0){
      this.setData({
        initemdata:JSON.parse(options.fieldValue) 
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    let pages = getCurrentPages();
    const beforePage = pages[pages.length - 2]
    if(this.data.initemdata.length > 0){
      beforePage.setData({
          fieldValue:this.data.initemdata,
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})